<?php


namespace App\Http\Controllers;


use Illuminate\Http\Request;

class UploadController extends Controller
{

    private $secret = 'D_qW18uimL40XTeZX2F0RCeeV6vyLEifZ9CMPsg2';
    private $accessKey = '0I7pO0vyyUcqaMOu51tr1wPfSX6gjxLbGDEWn0YQ';

    public function imageToken(Request $request){
        $this->validate($request, [
            'file' => 'required|string'
        ]);
        $auth = new \Qiniu\Auth($this->accessKey, $this->secret);
        $policy = ['deadline'=> time() + 3600];
        // 强制校验类型
        $policy['detectMime'] = 1;
        $policy['mimeLimit'] = 'image/jpeg;image/jpg;image/gif;image/png;image/webp;';
        $fileInfo = pathinfo($request->get('file'));
        $key = 'build/'.md5($fileInfo['filename']).'.'.(empty($fileInfo['extension']) ? 'jpg' : $fileInfo['extension'] );
        $host = 'http://static.mall.miaobe.cn/';
        return self::success([
            'key' => $key,
            'dest' => $host.$key,
            'token' => $auth->uploadToken('maimeng', $key, 3600, $policy),
        ]);
    }

}